/*
 * ﻿Copyright (C) 2013-2014 NewMain Softech
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package com.newmainsoftech.eatestutil.container;

import javax.ejb.embeddable.EJBContainer;
import javax.naming.Context;

public interface EjbContextCloseHandler {
	/**
	 * Getter for name of either directory of or .jar file of EJB module.<br />
	 * 
	 * @return Name of either directory of or .jar file of EJB module. 
	 */
	String getEjbModuleName();
//	void setEjbModuleName(final String ejbModuleName);
	
	/**
	 * Get instance of <code>EJBContainer</code>.<br />
	 * 
	 * @return Instance of <code>EJBContainer</code>.
	 */
	EJBContainer getEjbContainer();
//	void setEjbContainer(final EJBContainer ejbContainer);
	
	/**
	 * Get <code>Context</code> from <code>EJBContainer</code>.<br />
	 * 
	 * @return <code>Context</code> from <code>EJBContainer</code>.
	 */
	Context getContext();
//	void setContext(final Context context);

	
	// For closing EJB context and EJB container -------------------------------------------------- 
	/**
	 * To register consumer of EJB context and container available via the 
	 * <code>{@link #getContext()}</code> method and the <code>{@link #getEjbContainer()}</code> 
	 * method respectfully of this.<br /> 
	 * EJB context will be automatically closed when all consumers are removed via the  
	 * <code>{@link #removeConsumer(Object)}</code> method and the 
	 * <code>{@link #isClosedByNonConsumer()}</code> method returns true. 
	 * 
	 * @param comsumer Consumer object of EJB context and container.
	 */
	void registerConsumer( final Object comsumer);
	/**
	 * To remove EJB context (and container) consumer object registered via the 
	 * <code>{@link #registerConsumer(Object)}</code> method.<br />
	 * EJB context will be automatically closed when all consumers are removed via 
	 * this and the <code>{@link #isClosedByNonConsumer()}</code> method returns true. 
	 * 
	 * @param consumer Consumer object of EJB context and container.
	 * @see #setClosedByNonConsumer(boolean)
	 */
	void removeConsumer( final Object consumer);
	/**
	 * Provides the number of EJB context (and container) consumers.
	 *  
	 * @return the number of EJB context (and container) consumers.
	 */
	int getConsumerCount();	
	/**
	 * Indicates whether automatically close EJB context when all consumers are removed via 
	 * the <code>{@link #removeConsumer(Object)}</code> method.<br />
	 * 
	 * @return true to close EJB context automatically when all consumers are removed via 
	 * the <code>{@link #removeConsumer(Object)}</code> method, otherwise false.
	 * @see #close()
	 */
	boolean isClosedByNonConsumer();
	/**
	 * Set whether automatically close EJB context when all consumers are removed via 
	 * the <code>{@link #removeConsumer(Object)}</code> method.<br />
	 * 
	 * @param isClosedByNonConsumer : true to close EJB context automatically when all consumers 
	 * are removed via the <code>{@link #removeConsumer(Object)}</code> method, otherwise false.
	 * @see #close()
	 */
	void setClosedByNonConsumer( boolean isClosedByNonConsumer);
	/**
	 * Indicates whether EJB container is also closed automatically after closing EJB context.<br />
	 *    
	 * @return true to close EJB container as well as EJB context, otherwise false.
	 * @see #close()
	 * @see #setClosedByNonConsumer(boolean)
	 */
	boolean isContainerClosedToo();
	/**
	 * Set whether EJB container is also closed automatically after closing EJB context.<br />
	 * 
	 * @param isContainerClosedToo : true to close EJB container as well as EJB context, otherwise false.
	 * @see #close()
	 * @see #setClosedByNonConsumer(boolean)
	 */
	void setContainerClosedToo( boolean isContainerClosedToo);
	/**
	 * Getter of setting value of delay duration to hold starting close operation of EJB context.<br />
	 * 
	 * @return Duration in millisecond to hold starting close operation of EJB context.
	 * @see #close()
	 * @see #setClosedByNonConsumer(boolean)
	 */
	long getCloseDelay();
	/**
	 * Setter of setting value of delay duration to hold starting close operation of EJB context.<br />
	 * 
	 * @param closeDelay Duration in millisecond to hold starting close operation of EJB context.
	 * @see #close()
	 * @see #setClosedByNonConsumer(boolean)
	 */
	void setCloseDelay( long closeDelay);
	/**
	 * To close EJB context available via the <code>{@link #getContext()}</code> method.<br />
	 * 
	 * @see #removeConsumer(Object)
	 * @see #setClosedByNonConsumer(boolean)
	 */
	void close();
	/**
	 * To cancel scheduled task for closing EJB context.<br />
	 * @see #close()
	 * @see #setClosedByNonConsumer(boolean)
	 */
	void cancelClose();
	/**
	 * Indicates whether EJB context available via the <code>{@link #getContext()}</code> method  
	 * has been already closed.
	 * 
	 * @return true when EJB context has already been closed before, otherwise false.
	 */
	boolean hasBeenClosed();
	// --------------------------------------------------------------------------------------------
}